package com.cyclingAssistant.TTS;

import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.cyclingAssistant.project.R;

public class TTSTrial extends Activity implements OnInitListener {

	EditText text;
	Button speakButton;
	int REQ_TTS_STATUS_CHECK = 0;
	TextToSpeech mTts;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tts_layout);

		text = (EditText) findViewById(R.id.tts_text);
		speakButton = (Button) findViewById(R.id.tts_button_speak);
		speakButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mTts.speak(text.getText().toString(), TextToSpeech.QUEUE_ADD, null);
			}
		});

		// 检查TTS数据是否已经安装并且可用
		Intent checkIntent = new Intent();
		checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
		startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK); // When this  activity exits, your onActivityResult() method will be called with the given requestCode

	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQ_TTS_STATUS_CHECK) {
			switch (resultCode) {
			case TextToSpeech.Engine.CHECK_VOICE_DATA_PASS:
			// 这个返回结果表明TTS Engine可以用
			{
				mTts = new TextToSpeech(this, this);
				Log.v("installed", "TTS Engine is installed!");

			}

				break;
			case TextToSpeech.Engine.CHECK_VOICE_DATA_BAD_DATA:
				// 需要的语音数据已损坏
			case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA:
				// 缺少需要语言的语音数据
			case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_VOLUME:
			// 缺少需要语言的发音数据
			{
				// 这三种情况都表明数据有错,重新下载安装需要的数据
				Log.v("not installed", "Need language stuff:" + resultCode);
				Intent dataIntent = new Intent();
				dataIntent
						.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
				startActivity(dataIntent);

			}
				break;
			case TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL:
				// 检查失败
			default:
				Log.v("failure", "Got a failure. TTS apparently not available");
				break;
			}
		} else {
			// 其他Intent返回的结果
		}
	}

	@Override
	public void onInit(int status) {
		// TODO Auto-generated method stub
		// TTS Engine初始化完成
		if (status == TextToSpeech.SUCCESS) {
			int result = mTts.setLanguage(Locale.US);
			// 设置发音语言
			if (result == TextToSpeech.LANG_MISSING_DATA
					|| result == TextToSpeech.LANG_NOT_SUPPORTED)
			// 判断语言是否可用
			{
				Log.v("language", "Language is not available");
				speakButton.setEnabled(false);
			} else {
				mTts.speak("This is an example of speech synthesis.",
						TextToSpeech.QUEUE_ADD, null);
				speakButton.setEnabled(true);
			}
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if (mTts != null)
		// activity暂停时也停止TTS
		{
			mTts.stop();
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		// 释放TTS的资源
		mTts.shutdown();
	}

}
